package com.neu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neu.dao.UserDao;
import com.neu.daoimpl.UserDaoImpl;
import com.neu.entity.User;

/**
 * Servlet implementation class CookieTimeServlet
 */
@WebServlet("/CookieTimeServlet")
public class CookieTimeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieTimeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		/*
		 *   获取  Cookie
		 */
		Cookie[] cs = request.getCookies();
		Cookie c = findCookie(cs, "time");
		/*
		 * 获取当前系统的时间
		 */
		long t = System.currentTimeMillis();
		/*
		 * 判断 c 是否为null,如果为 null 则为第一次访问(也就是新用户)
		 */
		if(c==null) {
			/*
			 *  创建 Cookie 对象,存储当前访问的系统时间
			 */
			c=new Cookie("time", t+"");
			out.write("您是第一次访问");
		}else {
			String value = c.getValue();
			/*
			 *  将获取到的时间进行格式转换
			 */
			Date d = new Date();
			d.setTime(Long.parseLong(value));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = sdf.format(d);
			/*
			 * 将当前的时间添加到 Cookie 中
			 */
			c.setValue(t+"");
			UserDao ud = new UserDaoImpl();
			try {
				ud.insert(new User(0,format));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.write("您上一次的访问时间：:"+format);
		}
		/*
		 *  将   c 对象发送给浏览器
		 */
		response.addCookie(c);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	private Cookie findCookie(Cookie[] cs,String name) {
		/*
		 * 判断传进来的Cookie 是否为 null;
		 */
		if(cs==null) {
			/*
			 * 如果条件成立了  那么就说明数组不存在
			 * 			 也就是说你是第一次访问的浏览器
			 */
			return null;
		}
		/*
		 * 如果上面的不成立说明  数组Cookie[] 里面有值 也就是说不是第一次访问的浏览器
		 *   所以循环 Cookie 数组, 根据指定的参数 name 找到对应的 Cookie 并返回
		 */
		for(Cookie c :cs) {
			if(c.getName().equals(name)) {
				return c;
			}
		}
		return null;
		
	}

}
